//
//  ByPersonalEmailModifyVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/8/3.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByPersonalEmailModifyVC.h"

#import "ByPersonalViewModel.h"             // vm


@interface ByPersonalEmailModifyVC () <ByPersonalUpdateDelegate>

@property (nonatomic, strong) ByPersonalViewModel *vm;

@property (nonatomic, strong) UITextField *emailTF;
@property (nonatomic, strong) UIButton *confirmButton;

@end

@implementation ByPersonalEmailModifyVC

- (ByPersonalViewModel *)vm {
    if (!_vm) {
        _vm = [[ByPersonalViewModel alloc] init];
        _vm.updateDelegate = self;
    }
    return _vm;
}

#pragma viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改邮箱";
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self createLeftBackBarButtonItem];
    
    [self createView];
}


#pragma mark - Net request
- (void)updateSuccess:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertSuccess:result.msg];

    ByNetUserModel *user = BY_USER;
    user.email = self.emailTF.text;
    /** 存储登录信息 */
    [ByCommonCache setUserInfo:user];
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)updateFailure:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertError:result.msg];
}

#pragma mark - create view
- (void)createView {
    
    /** 输入框 */
    self.emailTF = [[UITextField alloc] init];
    self.emailTF.frame = CGRectMake(20, 20, SCREEN_WIDTH - 40, 40);
    self.emailTF.tintColor = JM_THEME_COLOR;
    self.emailTF.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.emailTF];
    
    /** 确认按钮 */
    [BoyeViewHandle createButtonInView:self.view config:^(UIButton *button) {
        button.frame = CGRectMake(SCREEN_WIDTH * 0.1, self.emailTF.bottom + 30, SCREEN_WIDTH * 0.8, 30);
        button.backgroundColor = JM_THEME_COLOR;
        button.titleLabel.font = FONT(16);
        [button setTitle:@"确认修改" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 4;
    }];
}

#pragma mark - 点击事件
- (void)confirmClick {
    
    if (![self.emailTF.text isMatchPredicateWithRegex:[NSString getRegexStringWithType:EMAIL]]) {
        [ByCoreToolAlert alertError:@"请输入正确的邮箱"];
        return;
    }
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.email = self.emailTF.text;
    [self.vm update:model];
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
